<?php

declare(strict_types=1);

namespace App\RoomWelcome\Logic;

use Hyperf\Di\Annotation\Inject;
use App\User\Service\UserService;
use App\RoomWelcome\Service\RoomWelcomeService;

/**
 * 入群欢迎语-列表.
 *
 * Class IndexLogic
 */
class IndexLogic
{
    /**
     * @Inject
     * @var RoomWelcomeService
     */
    protected $roomWelcomeService;

    /**
     * @Inject
     * @var UserService
     */
    protected $userService;

    /**
     * @var int
     */
    protected $page;

    /**
     * @var int
     */
    protected $perPage = 10;

    /**
     * @param array $params 请求参数
     * @return array 响应数组
     */
    public function handle(array $params): array
    {
        ## 处理请求参数
        $params = $this->handleParams($params);
        ## 查询数据
        return $this->getroomWelcomesList($params);
    }

    /**
     * 处理参数.
     * @param array $params 接受参数
     * @return array 响应数组
     */
    private function handleParams(array $params): array
    {
        $where = [];
        empty($params['text']) || $where[] = ['msg_text', 'LIKE', '%' . $params['text'] . '%'];
        $user = user();
        $where['corp_id'] = $user['corp_id'];
        //if ($user['isSuperAdmin'] === 0) {
        //    $where['create_user_id'] = $user['id'];
        //}
        $options = [
            'perPage' => $params['perPage'],
            'page' => $params['page'],
            'orderByRaw' => 'id desc',
        ];

        return ['where' => $where, 'options' => $options];
    }

    /**
     * 获取欢迎语列表.
     * @param array $params 参数
     * @return array 响应数组
     */
    private function getroomWelcomesList(array $params): array
    {
        $columns = ['id', 'complex_type', 'msg_text', 'msg_complex', 'create_user_id', 'create_time'];
        $roomWelcomeList = $this->roomWelcomeService->getRoomWelcomeList($params['where'], $columns, $params['options']);
        $list = [];
        $data = [
            'size' => $this->perPage,
            'total' => 0,
            'pages' => 0,
            'current' => $params['options']['page'],
        ];
        if(!empty($roomWelcomeList['data'])) {
            foreach ($roomWelcomeList['data'] as $key => $val) {
                //处理创建者信息
                //$username = $this->userService->getUserById($val['createUserId']);
                $msgComplex = json_decode($val['msgComplex'], true, 512, JSON_THROW_ON_ERROR);
                if (!empty($msgComplex['pic'])) {
                    $msgComplex['pic'] = file_full_url($msgComplex['pic']);
                }
                $list[$key] = [
                    'id' => $val['id'],
                    'msgText' => $val['msgText'],
                    'complexType' => $val['complexType'],
                    'msgComplex' => json_encode($msgComplex, JSON_THROW_ON_ERROR),
                    'createUser' => $val['createUserId'] ?? "",
                    'createTime' => $val['createTime'],
                ];
            }
        }
        $data['records'] = $list;
        return $data;
    }

}
